package com.zy.core.web;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.zy.core.model.QueryResult;
//import com.zy.pro.logManage.LogManageCnt;
//import com.zy.pro.logManage.model.Log;
//import com.zy.pro.logManage.service.LogService;
import com.zy.pro.system.model.Employee;
//import com.zy.pro.system.model.Role;
import com.zy.pro.utils.BaseConst;
import com.zy.utils.SessionUtils;
import com.zy.utils.UUIDHelper;
/**
 * 控制器基类
 * @author JiangYingxu
 *
 */
public class BaseController {
	//protected Log log = new Log(); 

	static {
		//定义允许上传的文件扩展名
		HashMap<String, String> extMap = new HashMap<String, String>();
		extMap.put("image", "gif,jpg,jpeg,png,bmp");
		extMap.put("flash", "swf,flv");
		extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
		extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");

	}
	
	/**
	 * 日志服务类
	 */
	//@Resource(name = "logServiceImpl")
	//protected LogService logService;
	
	
	
	private static Map<String, Object> map = new HashMap<String, Object>();
	
	public static <T> Map<String, Object> sendClient(QueryResult<T> qr){
		if(qr != null){
			map.put("total", qr.getTotalrecord());
			map.put("rows", qr.getResultlist());
		}
		return map;
	}
	
	public static <T> Map<String, Object> sendClient(String key, Object value){
		map.put(key, value);
		return map;
	}
	
	/**
	 * 添加登录日志
	 * @throws Exception 
	 */
	/*private void insertLog(Integer houseId, Integer status, String message, HttpServletRequest request, String exception) throws Exception {
		if(houseId == null){
			houseId = -1;
		}
		log.setHouseId(houseId);
		log.setSystemType(LogManageCnt.LOG_TYPE_HOUSE);
		log.setStatus(status);
		log.setMessage(message);
		log.setException(exception);
		String name = this.getClass().getName();
		name = name.substring(name.lastIndexOf(".") + 1);
		log.setControlName(name);
		log.setMethodName(Thread.currentThread().getStackTrace()[3].getMethodName());
		log = logger(log, request);
		
		logService.save(log);
	}*/
	
	/**
	 * 添加登录日志（普通）
	 * @throws Exception 
	 */
	/*protected void insertLogInfo(Integer houseId, String message, HttpServletRequest request) throws Exception {
		insertLog(houseId, BaseConst.LOG_STATUS_INFO, message, request, null);
	}*/
	
	/**
	 * 添加登录日志2（普通）
	 * @throws Exception 
	 */
	/*protected void insertLogInfo(String message, HttpServletRequest request) throws Exception {
		Role role = SessionUtils.getRole(request);
		insertLog(role.getHouseId(), BaseConst.LOG_STATUS_INFO, message, request, null);
	}*/

	/**
	 * 添加登录日志（警告）
	 * @throws Exception 
	 */
	/*protected void insertLogWarn(Integer houseId, String message, HttpServletRequest request) throws Exception {
		insertLog(houseId, BaseConst.LOG_STATUS_WARN, message, request, null);
	}*/
	
	/**
	 * 添加登录日志2（警告）
	 * @throws Exception 
	 */
	/*protected void insertLogWarn(String message, HttpServletRequest request) throws Exception {
		Role role = SessionUtils.getRole(request);
		insertLog(role.getHouseId(), BaseConst.LOG_STATUS_WARN, message, request, null);
	}*/
	
	/**
	 * 添加登录日志（错误）
	 * @param string
	 * @throws Exception 
	 */
	/*protected void insertLogError(Integer houseId, String message, HttpServletRequest request, Exception e) {
		try {
			insertLog(houseId, BaseConst.LOG_STATUS_ERROR, message, request, e.getMessage());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}*/
	/**
	 * 添加登录日志2（错误）
	 * @param string
	 * @throws Exception 
	 */
	/*protected void insertLogError(String message, HttpServletRequest request, Exception e) {
		Role role = SessionUtils.getRole(request);
		try {
			insertLog(role.getHouseId(), BaseConst.LOG_STATUS_ERROR, message, request, e.getMessage());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}*/
	
	
	/**
	 * 日志输入值
	 * @param log
	 * @param request
	 */
	/*public static Log logger(Log log, HttpServletRequest request){
		log.setLogId(UUIDHelper.getUUID36());
		log.setIp(request.getRemoteAddr());
		Employee user = SessionUtils.getUser(request);
		if(user != null){
			log.setUserName(user.getUsername());
		}else{
			log.setUserName("未知用户");
		}
		log.setHost(String.valueOf(request.getServerPort()));
		log.setCreateTime(new Date());
		return log;
	}*/
	
	 /**
	  * 获取IP地址
	  * @param request
	  * @return
	  */
	 public String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
	 
	public static String getBasePath(HttpServletRequest request){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
		return basePath;
	}
}
